#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "util/CStrTool.h"
#include "CIpodLog.h"
#include "CIpodConfig.h"

#define BUF_SIZE            1024

int CIpodConfig::s_Mode     = Mode_MEDIA;
int CIpodConfig::s_ConnMode = ConnMode_USB_DEVICE; //ipod与车机连接方式
int CIpodConfig::s_Protocol = IPODProtocol::ALL;
IPODIdentification CIpodConfig::s_Identification;

int CIpodConfig::init(int mode, const char *cfgPath, int connMode,int hidg) {
    CIpodLog::i("CIpodConfig::init, mode = %d, cfgPath = %s\n", mode, cfgPath);

    s_Mode = mode;
    s_ConnMode = connMode;
    if (cfgPath == NULL) {
        CIpodLog::e("CIpodConfig::init, cfgPath is NULL!\n");
        return -1;
    }else{
        FILE *cfgFile = fopen(cfgPath, "r");
        if (cfgFile == NULL) {
            CIpodLog::e("CIpodConfig::init, cfgFile open failed!\n");

            s_Identification.usbDeviceHIDs.clear();
            s_Identification.supportedLanguages.clear();
            s_Identification.usbHostHIDs.clear();
            s_Identification.wirelessCarPlayTransports.clear();
            s_Identification.usbDeviceTransport.rates.clear();
            s_Identification.vehicleInfo.engines.clear();

            //默认参数
            s_Protocol = 2;

            // "<Identification>"
            s_Identification.name = "SharkCarPlay";
            s_Identification.modelIdentifier = "23MM";
            s_Identification.manufacturer = "iAuto Electronics Co., Ltd";
            s_Identification.serialNumber = "SN8906120011";
            s_Identification.firmwareVersion = "V01.00";
            s_Identification.hardwareVersion = "V01";
            s_Identification.productPlanUID = "c9ab63cccc1a477a";
            // "<Messages>"
            s_Identification.messages.app = 1;
            s_Identification.messages.touch = 1;
            s_Identification.messages.bluetooth = 1;
            s_Identification.messages.communications = 1;
            s_Identification.messages.authentication = 1;
            s_Identification.messages.notifications = 1;
            s_Identification.messages.eap = 1;
            s_Identification.messages.hid = 1;
            s_Identification.messages.location = 1;
            s_Identification.messages.media = 1;
            s_Identification.messages.playing = 1;
            s_Identification.messages.power = 1;
            s_Identification.messages.audio = 1;
            s_Identification.messages.vehicle = 1;
            s_Identification.messages.voiceover = 1;
            s_Identification.messages.wifi = 0;
            s_Identification.messages.serialnumber = 1;
            s_Identification.messages.oob = 1;
            s_Identification.power = 2;
            s_Identification.maxCurrDrawn = 0;
            s_Identification.appTeamId = "12345678";
            s_Identification.currentLanguage = "en";
            std::string valueLanguage = "en zh";
            CStrTool::split(valueLanguage, s_Identification.supportedLanguages, "|");
            s_Identification.eaprotocol.id = 1;
            s_Identification.eaprotocol.name = "com.baidu.CarLifeVehicleProtocol";
            s_Identification.eaprotocol.action = 1;
            s_Identification.eaprotocol.nativeid = 3;
            s_Identification.eaprotocol.carplay = 1;
            s_Identification.serialTransport.id = 0;
            s_Identification.serialTransport.name = "SerialTransport";
            s_Identification.serialTransport.connection =0;
            s_Identification.usbHostTransport.id = 3;
            s_Identification.serialTransport.name = "XQ-USBHost";
            s_Identification.serialTransport.connection = 1;
            s_Identification.usbHostTransport.number = 1;
            s_Identification.usbHostTransport.carplay = 1;
            s_Identification.bluetoothTransport.id = 7;
            s_Identification.bluetoothTransport.name = "XQ-Bluetooth";
            s_Identification.bluetoothTransport.connection =1;
            CIpodLog::i("CIpodConfig::init, end and success!\n");
//            debugConfig();
        }else{
            s_Identification.usbDeviceHIDs.clear();
            s_Identification.supportedLanguages.clear();
            s_Identification.usbHostHIDs.clear();
            s_Identification.wirelessCarPlayTransports.clear();
            s_Identification.usbDeviceTransport.rates.clear();
            s_Identification.vehicleInfo.engines.clear();

            char line[BUF_SIZE];
            char subline[BUF_SIZE];
            while (fgets(line, BUF_SIZE - 1, cfgFile) != NULL) {
                if (strstr(line, "<GeneralConfig>") != NULL) {
                    while (fgets(subline, BUF_SIZE - 1, cfgFile) != NULL) {
                        if (strstr(subline, "ProtocolVersion") != NULL) {
                            s_Protocol = atoi(CStrTool::value(subline).c_str());
                        }

                        if (strstr(subline, "</GeneralConfig>") != NULL) {
                            break;
                        }
                    }
                }

                if (strstr(line, "<Identification>") != NULL) {
                    while (fgets(subline, BUF_SIZE - 1, cfgFile) != NULL) {
                        if (strstr(subline, "Name") != NULL) {
                            s_Identification.name = CStrTool::value(subline);
                        }

                        if (strstr(subline, "ModelIdentifier") != NULL) {
                            s_Identification.modelIdentifier = CStrTool::value(subline);
                        }

                        if (strstr(subline, "Manufacturer") != NULL) {
                            s_Identification.manufacturer = CStrTool::value(subline);
                        }

                        if (strstr(subline, "SerialNumber") != NULL) {
                            s_Identification.serialNumber = CStrTool::value(subline);
                        }

                        if (strstr(subline, "FirmwareVersion") != NULL) {
                            s_Identification.firmwareVersion = CStrTool::value(subline);
                        }

                        if (strstr(subline, "HardwareVersion") != NULL) {
                            s_Identification.hardwareVersion = CStrTool::value(subline);
                        }

                        if (strstr(subline, "ProductPlanUID") != NULL) {
                            s_Identification.productPlanUID = CStrTool::value(subline);
                        }

                        if (strstr(subline, "Messages") != NULL) {
                            s_Identification.messages.value = 0;
                            std::string value = CStrTool::value(subline);
                            std::list<std::string> messages;
                            CStrTool::split(value, messages, "|");
                            for (std::list<std::string>::iterator iter = messages.begin(); iter != messages.end(); ++iter) {
                                if (iter->compare("app") == 0) {
                                    s_Identification.messages.app = 1;
                                } else if (iter->compare("touch") == 0) {
                                    s_Identification.messages.touch = 1;
                                } else if (iter->compare("bluetooth") == 0) {
                                    s_Identification.messages.bluetooth = 1;
                                } else if (iter->compare("communications") == 0)
                                {
                                    if( connMode == ConnMode_BLUETOOTH )
                                    {
                                        s_Identification.messages.communications = 0;
                                    }
                                    else    s_Identification.messages.communications = 1;
                                }
                                else if (iter->compare("authentication") == 0) {
                                    s_Identification.messages.authentication = 1;
                                } else if (iter->compare("notifications") == 0) {
                                    s_Identification.messages.notifications = 1;
                                } else if (iter->compare("eap") == 0) {
                                    s_Identification.messages.eap = 1;
                                } else if (iter->compare("hid") == 0) {
                                    s_Identification.messages.hid = 1;
                                } else if (iter->compare("location") == 0)
                                {
                                    if( connMode == ConnMode_BLUETOOTH )
                                    {
                                        CIpodLog::i("iAP2 over the Bluetooth transport should not declare Location.\n");
                                        s_Identification.messages.location = 0;
                                    }
                                    else    s_Identification.messages.location = 1;
                                }
                                else if (iter->compare("media") == 0)
                                {
                                    if( connMode != ConnMode_BLUETOOTH )  s_Identification.messages.media = 1;
                                }
                                else if (iter->compare("playing") == 0)
                                {
                                    if( connMode != ConnMode_BLUETOOTH )  s_Identification.messages.playing = 1;
                                }
                                else if (iter->compare("power") == 0) {
                                    if(connMode == ConnMode_BLUETOOTH || connMode == ConnMode_WiFi )
                                    {
                                        CIpodLog::i("iAP2 over the Bluetooth or WiFi transport should not declare PowerSourceUpdate.\n");
                                        s_Identification.messages.power = 0;
                                    } else {
                                        s_Identification.messages.power = 1;
                                    }
                                } else if (iter->compare("audio") == 0) {
                                    s_Identification.messages.audio = 1;
                                } else if (iter->compare("vehicle") == 0) {
                                    s_Identification.messages.vehicle = 1;
                                } else if (iter->compare("voice") == 0) {
                                    s_Identification.messages.voiceover = 1;
                                } else if (iter->compare("wifi") == 0) {
                                    s_Identification.messages.wifi = 1;
                                }else if (iter->compare("serialnumber") == 0) {
                                    s_Identification.messages.serialnumber = 1;
                                }else if (iter->compare("oob") == 0)
                                {
                                    if( connMode == ConnMode_USB_HOST )
                                    {
                                        s_Identification.messages.oob = 1;
                                    }
                                    else    s_Identification.messages.oob = 0;
                                }
                            }
                        }

                        if (strstr(subline, "PowerProvidingCapability") != NULL) {
                            if(connMode == ConnMode_BLUETOOTH || connMode == ConnMode_WiFi ){
                                CIpodLog::i("iAP2 over the Bluetooth or WiFi transport should not declare PowerSourceUpdate for PowerProvidingCapability.\n");
                                s_Identification.power = 0;
                            } else {
                                s_Identification.power = atoi(CStrTool::value(subline).c_str());
                            }
                        }

                        if (strstr(subline, "MaximumCurrentDrawnFromDevice") != NULL) {
                            s_Identification.maxCurrDrawn = atoi(CStrTool::value(subline).c_str());
                        }

                        if (strstr(subline, "AppMatchTeamID") != NULL) {
                            s_Identification.appTeamId = CStrTool::value(subline);
                        }

                        if (strstr(subline, "CurrentLanguage") != NULL) {
                            s_Identification.currentLanguage = CStrTool::value(subline);
                        }

                        if (strstr(subline, "SupportedLanguage") != NULL) {
                            std::string value = CStrTool::value(subline);
                            CStrTool::split(value, s_Identification.supportedLanguages, "|");
                        }
                        if (strstr(subline, "ExternalAccessoryProtocol") != NULL) {
                            std::list<std::string> eapValues;
                            CStrTool::split(subline, eapValues, "ipod:");
                            for (std::list<std::string>::iterator iter = eapValues.begin(); iter != eapValues.end(); ++iter) {
                                if (iter->find("=") != std::string::npos) {
                                    std::string key = iter->substr(0, iter->find("="));
                                    std::string value = CStrTool::value(iter->data());
                                    if (key.compare("id") == 0) {
                                        if(connMode == ConnMode_USB_HOST){
                                            s_Identification.eaprotocol.id = atoi(value.data());
                                        }else{
                                            s_Identification.eaprotocol.id = 0;
                                        }
                                    } else if (key.compare("name") == 0) {
                                        s_Identification.eaprotocol.name = value;
                                    } else if (key.compare("action") == 0) {
                                        s_Identification.eaprotocol.action = atoi(value.data());
                                    } else if (key.compare("nativeid") == 0) {
                                        s_Identification.eaprotocol.nativeid = atoi(value.data());
                                    } else if (key.compare("carplay") == 0) {
                                        s_Identification.eaprotocol.carplay = atoi(value.data());
                                    }
                                }
                            }
                        }
                        if (strstr(subline, "SerialTransportComponent") != NULL) {
                            std::list<std::string> serailTransportValues;
                            CStrTool::split(subline, serailTransportValues, "ipod:");
                            for (std::list<std::string>::iterator iter = serailTransportValues.begin(); iter != serailTransportValues.end(); ++iter) {
                                if (iter->find("=") != std::string::npos) {
                                    std::string key = iter->substr(0, iter->find("="));
                                    std::string value = CStrTool::value(iter->data());
                                    if (key.compare("id") == 0) {
                                        s_Identification.serialTransport.id = atoi(value.data());
                                    } else if (key.compare("name") == 0) {
                                        s_Identification.serialTransport.name = value;
                                    } else if (key.compare("connection") == 0) {
                                        s_Identification.serialTransport.connection = atoi(value.data());
                                    }
                                }
                            }
                        }

                        if (strstr(subline, "USBDeviceTransportComponent") != NULL) {
                            std::list<std::string> usbDeviceTransportValues;
                            CStrTool::split(subline, usbDeviceTransportValues, "ipod:");
                            for (std::list<std::string>::iterator iter = usbDeviceTransportValues.begin(); iter != usbDeviceTransportValues.end(); ++iter) {
                                if (iter->find("=") != std::string::npos) {
                                    std::string key = iter->substr(0, iter->find("="));
                                    std::string value = CStrTool::value(iter->data());
                                    if (key.compare("id") == 0) {
                                        s_Identification.usbDeviceTransport.id = atoi(value.data());
                                    } else if (key.compare("name") == 0) {
                                        s_Identification.usbDeviceTransport.name = value;
                                    } else if (key.compare("connection") == 0) {
                                        s_Identification.usbDeviceTransport.connection = atoi(value.data());
                                    } else if (key.compare("rate") == 0) {
                                        std::list<std::string> rateValues;
                                        CStrTool::split(value, rateValues, "|");
                                        for (std::list<std::string>::iterator rateIter = rateValues.begin(); rateIter != rateValues.end(); ++rateIter) {
                                            if (rateIter->compare("8000HZ") == 0) {
                                                s_Identification.usbDeviceTransport.rates.push_back(IPODSampleRate::RATE_8000HZ);
                                            } else if (rateIter->compare("11025HZ") == 0) {
                                                s_Identification.usbDeviceTransport.rates.push_back(IPODSampleRate::RATE_11025HZ);
                                            } else if (rateIter->compare("12000HZ") == 0) {
                                                s_Identification.usbDeviceTransport.rates.push_back(IPODSampleRate::RATE_12000HZ);
                                            } else if (rateIter->compare("16000HZ") == 0) {
                                                s_Identification.usbDeviceTransport.rates.push_back(IPODSampleRate::RATE_16000HZ);
                                            } else if (rateIter->compare("22050HZ") == 0) {
                                                s_Identification.usbDeviceTransport.rates.push_back(IPODSampleRate::RATE_22050HZ);
                                            } else if (rateIter->compare("24000HZ") == 0) {
                                                s_Identification.usbDeviceTransport.rates.push_back(IPODSampleRate::RATE_24000HZ);
                                            } else if (rateIter->compare("32000HZ") == 0) {
                                                s_Identification.usbDeviceTransport.rates.push_back(IPODSampleRate::RATE_32000HZ);
                                            } else if (rateIter->compare("44100HZ") == 0) {
                                                s_Identification.usbDeviceTransport.rates.push_back(IPODSampleRate::RATE_44100HZ);
                                            } else if (rateIter->compare("48000HZ") == 0) {
                                                s_Identification.usbDeviceTransport.rates.push_back(IPODSampleRate::RATE_48000HZ);
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        if (strstr(subline, "USBHostTransportComponent") != NULL ) {
                            std::list<std::string> usbHostTransportValues;
                            CStrTool::split(subline, usbHostTransportValues, "ipod:");
                            for (std::list<std::string>::iterator iter = usbHostTransportValues.begin(); iter != usbHostTransportValues.end(); ++iter) {
                                if (iter->find("=") != std::string::npos) {
                                    std::string key = iter->substr(0, iter->find("="));
                                    std::string value = CStrTool::value(iter->data());
                                    if (key.compare("id") == 0)
                                    {
                                        s_Identification.usbHostTransport.id = atoi(value.data());
                                    }
                                    else if (key.compare("name") == 0) {
                                        s_Identification.usbHostTransport.name = value;
                                    } else if (key.compare("connection") == 0) {
                                        s_Identification.usbHostTransport.connection = atoi(value.data());
                                    } else if (key.compare("number") == 0) {
                                        s_Identification.usbHostTransport.number = atoi(value.data());
                                    } else if (key.compare("carplay") == 0) {
                                        s_Identification.usbHostTransport.carplay = atoi(value.data());
                                    }
                                }
                            }
                        }

                        if (strstr(subline, "BluetoothTransportComponent") != NULL) {
                            std::list<std::string> bluetoothTransportValues;
                            CStrTool::split(subline, bluetoothTransportValues, "ipod:");
                            for (std::list<std::string>::iterator iter = bluetoothTransportValues.begin(); iter != bluetoothTransportValues.end(); ++iter) {
                                if (iter->find("=") != std::string::npos) {
                                    std::string key = iter->substr(0, iter->find("="));
                                    std::string value = CStrTool::value(iter->data());
                                    if (key.compare("id") == 0) {
                                        s_Identification.bluetoothTransport.id = atoi(value.data());
                                    } else if (key.compare("name") == 0) {
                                        s_Identification.bluetoothTransport.name = value;
                                    } else if (key.compare("connection") == 0) {
                                        s_Identification.bluetoothTransport.connection = atoi(value.data());
                                    }
                                }
                            }
                        }

                        if (strstr(subline, "USBDeviceHIDComponent") != NULL) {
                            USBDeviceHIDComponent usbDeviceHID;
                            std::list<std::string> usbDeviceHIDValues;
                            CStrTool::split(subline, usbDeviceHIDValues, "ipod:");
                            for (std::list<std::string>::iterator iter = usbDeviceHIDValues.begin(); iter != usbDeviceHIDValues.end(); ++iter) {
                                if (iter->find("=") != std::string::npos) {
                                    std::string key = iter->substr(0, iter->find("="));
                                    std::string value = CStrTool::value(iter->data());
                                    if (key.compare("id") == 0) {
                                        usbDeviceHID.id = atoi(value.data());
                                    } else if (key.compare("name") == 0) {
                                        usbDeviceHID.name = value;
                                    } else if (key.compare("function") == 0) {
                                        usbDeviceHID.function = atoi(value.data());
                                    }
                                }
                            }
                            s_Identification.usbDeviceHIDs.push_back(usbDeviceHID);
                        }

                        if (strstr(subline, "VehicleInformationComponent") != NULL) {
                            std::list<std::string> vehicleInfoValues;
                            CStrTool::split(subline, vehicleInfoValues, "ipod:");
                            for (std::list<std::string>::iterator iter = vehicleInfoValues.begin(); iter != vehicleInfoValues.end(); ++iter) {
                                if (iter->find("=") != std::string::npos) {
                                    std::string key = iter->substr(0, iter->find("="));
                                    std::string value = CStrTool::value(iter->data());
                                    if (key.compare("id") == 0) {
                                        s_Identification.vehicleInfo.id = atoi(value.data());
                                    } else if (key.compare("name") == 0) {
                                        s_Identification.vehicleInfo.name = value;
                                    } else if (key.compare("engine") == 0) {
                                        std::list<std::string> engineValues;
                                        CStrTool::split(value, engineValues, "|");
                                        for (std::list<std::string>::iterator engineIter = engineValues.begin(); engineIter != engineValues.end(); ++engineIter) {
                                            if (engineIter->compare("gasoline") == 0) {
                                                s_Identification.vehicleInfo.engines.push_back(IPODEngine::GASOLINE);
                                            } else if (engineIter->compare("diesel") == 0) {
                                                s_Identification.vehicleInfo.engines.push_back(IPODEngine::DIESEL);
                                            } else if (engineIter->compare("electric") == 0) {
                                                s_Identification.vehicleInfo.engines.push_back(IPODEngine::ELECTRIC);
                                            } else if (engineIter->compare("cng") == 0) {
                                                s_Identification.vehicleInfo.engines.push_back(IPODEngine::CNG);
                                            }
                                        }
                                    } else if (key.compare("display") == 0) {
                                        s_Identification.vehicleInfo.display = value;
                                    }
                                }
                            }
                        }

                        if (strstr(subline, "VehicleStatusComponent") != NULL) {
                            std::list<std::string> vehicleStatusValues;
                            CStrTool::split(subline, vehicleStatusValues, "ipod:");
                            for (std::list<std::string>::iterator iter = vehicleStatusValues.begin(); iter != vehicleStatusValues.end(); ++iter) {
                                if (iter->find("=") != std::string::npos) {
                                    std::string key = iter->substr(0, iter->find("="));
                                    std::string value = CStrTool::value(iter->data());
                                    if (key.compare("id") == 0) {
                                        s_Identification.vehicleStatus.id = atoi(value.data());
                                    } else if (key.compare("name") == 0) {
                                        s_Identification.vehicleStatus.name = value;
                                    } else if (key.compare("status") == 0) {
                                        std::list<std::string> statusValues;
                                        CStrTool::split(value, statusValues, "|");
                                        for (std::list<std::string>::iterator statusIter = statusValues.begin(); statusIter != statusValues.end(); ++statusIter) {
                                            if (statusIter->compare("range") == 0) {
                                                s_Identification.vehicleStatus.range = true;
                                            } else if (statusIter->compare("temperature") == 0) {
                                                s_Identification.vehicleStatus.temperature = true;
                                            } else if (statusIter->compare("warning") == 0) {
                                                s_Identification.vehicleStatus.warning = true;
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        if (strstr(subline, "LocationInformationComponent") != NULL && connMode != ConnMode_BLUETOOTH ) {
                            std::list<std::string> locationValues;
                            CStrTool::split(subline, locationValues, "ipod:");
                            for (std::list<std::string>::iterator iter = locationValues.begin(); iter != locationValues.end(); ++iter) {
                                if (iter->find("=") != std::string::npos) {
                                    std::string key = iter->substr(0, iter->find("="));
                                    std::string value = CStrTool::value(iter->data());
                                    if (key.compare("id") == 0) {
                                        if(connMode == ConnMode_BLUETOOTH){
                                            s_Identification.locationInfo.id = 0;
                                        }else{
                                            s_Identification.locationInfo.id = atoi(value.data());
                                        }
                                    } else if (key.compare("name") == 0) {
                                        s_Identification.locationInfo.name = value;
                                    } else if (key.compare("nmea") == 0) {
                                        std::list<std::string> nmeaValues;
                                        CStrTool::split(value, nmeaValues, "|");
                                        for (std::list<std::string>::iterator nmeaIter = nmeaValues.begin(); nmeaIter != nmeaValues.end(); ++nmeaIter) {
                                            if (nmeaIter->compare("GPGGA") == 0) {
                                                s_Identification.locationInfo.GPGGA = true;
                                            } else if (nmeaIter->compare("GPRMC") == 0) {
                                                s_Identification.locationInfo.GPRMC = true;
                                            } else if (nmeaIter->compare("GPGSV") == 0) {
                                                s_Identification.locationInfo.GPGSV = true;
                                            } else if (nmeaIter->compare("PASCD") == 0) {
                                                s_Identification.locationInfo.PASCD = true;
                                            } else if (nmeaIter->compare("PAGCD") == 0) {
                                                s_Identification.locationInfo.PAGCD = true;
                                            } else if (nmeaIter->compare("PAACD") == 0) {
                                                s_Identification.locationInfo.PAACD = true;
                                            } else if (nmeaIter->compare("GPHDT") == 0) {
                                                s_Identification.locationInfo.GPHDT = true;
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        if (strstr(subline, "USBHostHIDComponent") != NULL) {
                            USBHostHIDComponent usbHostHID;
                            std::list<std::string> usbHostHIDValues;
                            CStrTool::split(subline, usbHostHIDValues, "ipod:");
                            for (std::list<std::string>::iterator iter = usbHostHIDValues.begin(); iter != usbHostHIDValues.end(); ++iter) {
                                if (iter->find("=") != std::string::npos) {
                                    std::string key = iter->substr(0, iter->find("="));
                                    std::string value = CStrTool::value(iter->data());
                                    if (key.compare("id") == 0) {
                                        usbHostHID.id = atoi(value.data());
                                    } else if (key.compare("name") == 0) {
                                        usbHostHID.name = value;
                                    } else if (key.compare("function") == 0) {
                                        usbHostHID.function = atoi(value.data());
                                    } else if (key.compare("transportid") == 0) {
                                        usbHostHID.transportid = atoi(value.data());
                                    } else if (key.compare("number") == 0) {
                                        usbHostHID.number = atoi(value.data());
                                    }
                                }
                            }
                            s_Identification.usbHostHIDs.push_back(usbHostHID);
                        }

                        if (strstr(subline, "WirelessCarPlayTransportComponent") != NULL) {
                            WirelessCarPlayTransportComponent wirelessCarPlayTransport;
                            std::list<std::string> wirelessCarPlayValues;
                            CStrTool::split(subline, wirelessCarPlayValues, "ipod:");
                            for (std::list<std::string>::iterator iter = wirelessCarPlayValues.begin(); iter != wirelessCarPlayValues.end(); ++iter) {
                                if (iter->find("=") != std::string::npos) {
                                    std::string key = iter->substr(0, iter->find("="));
                                    std::string value = CStrTool::value(iter->data());
                                    if (key.compare("id") == 0) {
                                        wirelessCarPlayTransport.id = atoi(value.data());
                                    } else if (key.compare("name") == 0) {
                                        wirelessCarPlayTransport.name = value;
                                    } else if (key.compare("connection") == 0) {
                                        wirelessCarPlayTransport.connection = atoi(value.data());
                                    } else if (key.compare("carplay") == 0) {
                                        wirelessCarPlayTransport.carplay = atoi(value.data());
                                    }
                                }
                            }
                            s_Identification.wirelessCarPlayTransports.push_back(wirelessCarPlayTransport);
                        }

                        if (strstr(subline, "RouteGuidanceDisplayComponent") != NULL )
                        {
                            std::list<std::string> bluetoothHIDValues;
                            CStrTool::split(subline, bluetoothHIDValues, "ipod:");
                            for (std::list<std::string>::iterator iter = bluetoothHIDValues.begin(); iter != bluetoothHIDValues.end(); ++iter) {
                                if (iter->find("=") != std::string::npos) {
                                    std::string key = iter->substr(0, iter->find("="));
                                    std::string value = CStrTool::value(iter->data());
                                    if (key.compare("id") == 0)
                                    {
                                        if( connMode == ConnMode_BLUETOOTH )
                                        {
                                            s_Identification.routeGuidance.id = 0;
                                        }
                                        else    s_Identification.routeGuidance.id = atoi(value.data());
                                    }
                                    else if (key.compare("name") == 0) {
                                        s_Identification.routeGuidance.name = value;
                                    } else if (key.compare("maxcur") == 0) {
                                        s_Identification.routeGuidance.maxCurRoadNameLen = atoi(value.data());
                                    } else if (key.compare("maxdest") == 0) {
                                        s_Identification.routeGuidance.maxDestNameLen = atoi(value.data());
                                    }else if (key.compare("maxama") == 0) {
                                        s_Identification.routeGuidance.maxAManeRoadNameLen = atoi(value.data());
                                    } else if (key.compare("maxmane") == 0) {
                                        s_Identification.routeGuidance.maxManeDesLen = atoi(value.data());
                                    }else if (key.compare("maxgmdl") == 0) {
                                        s_Identification.routeGuidance.maxGuiManeStoCap = atoi(value.data());
                                    } else if (key.compare("maxlgdl") == 0) {
                                        s_Identification.routeGuidance.maxLaneGuiDesLen = atoi(value.data());
                                    }else if (key.compare("maxlgsc") == 0) {
                                        s_Identification.routeGuidance.maxLaneGuiStoCap = atoi(value.data());
                                    }
                                }
                            }
                        }

                        if (strstr(subline, "LocationInformationComponent") != NULL ) {
                            std::list<std::string> locationValues;
                            CStrTool::split(subline, locationValues, "ipod:");
                            for (std::list<std::string>::iterator iter = locationValues.begin(); iter != locationValues.end(); ++iter) {
                                if (iter->find("=") != std::string::npos) {
                                    std::string key = iter->substr(0, iter->find("="));
                                    std::string value = CStrTool::value(iter->data());
                                    if (key.compare("id") == 0)
                                    {
                                        if( connMode == ConnMode_BLUETOOTH )
                                        {
                                            s_Identification.locationInfo.id = 0;
                                        }
                                        else    s_Identification.locationInfo.id = atoi(value.data());

                                    } else if (key.compare("name") == 0) {
                                        s_Identification.locationInfo.name = value;
                                    } else if (key.compare("nmea") == 0) {
                                        std::list<std::string> nmeaValues;
                                        CStrTool::split(value, nmeaValues, "|");
                                        for (std::list<std::string>::iterator nmeaIter = nmeaValues.begin(); nmeaIter != nmeaValues.end(); ++nmeaIter) {
                                            if (nmeaIter->compare("GPGGA") == 0) {
                                                s_Identification.locationInfo.GPGGA = true;
                                            } else if (nmeaIter->compare("GPRMC") == 0) {
                                                s_Identification.locationInfo.GPRMC = true;
                                            } else if (nmeaIter->compare("GPGSV") == 0) {
                                                s_Identification.locationInfo.GPGSV = true;
                                            } else if (nmeaIter->compare("PASCD") == 0) {
                                                s_Identification.locationInfo.PASCD = true;
                                            } else if (nmeaIter->compare("PAGCD") == 0) {
                                                s_Identification.locationInfo.PAGCD = true;
                                            } else if (nmeaIter->compare("PAACD") == 0) {
                                                s_Identification.locationInfo.PAACD = true;
                                            } else if (nmeaIter->compare("GPHDT") == 0) {
                                                s_Identification.locationInfo.GPHDT = true;
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        if (strstr(subline, "</Identification>") != NULL) {
                            break;
                        }
                    }
                }
            }

            fclose(cfgFile);
            CIpodLog::i("CIpodConfig::init, end and success!\n");
            debugConfig();
        }
    }
    return 0;
}

int CIpodConfig::deinit(){

    s_Mode = Mode_MEDIA;
    s_ConnMode = ConnMode_USB_DEVICE;
    s_Protocol = IPODProtocol::ALL;
    s_Identification.supportedLanguages.clear();
    s_Identification.usbDeviceTransport.rates.clear();
    s_Identification.usbDeviceHIDs.clear();
    s_Identification.vehicleInfo.engines.clear();
    s_Identification.usbHostHIDs.clear();
    s_Identification.wirelessCarPlayTransports.clear();
    CIpodLog::i("CIpodConfig::deinit, exit ok!\n");

    return 0;
}

int CIpodConfig::connMode() {
    return s_ConnMode;
}

int CIpodConfig::getMode() {
    return s_Mode;
}

int CIpodConfig::getProtocol() {
    return s_Protocol;
}

IPODIdentification *CIpodConfig::getIdentification() {
    return &s_Identification;
}

void CIpodConfig::debugConfig() {
    CIpodLog::d("==================== IdentificationInformation ====================\n");
    CIpodLog::d("iAP2 control session version: %d\n", s_Protocol);
    CIpodLog::d("name = %s\n", s_Identification.name.c_str());
    CIpodLog::d("modelIdentifier = %s\n", s_Identification.modelIdentifier.c_str());
    CIpodLog::d("manufacturer = %s\n", s_Identification.manufacturer.c_str());
    CIpodLog::d("serialNumber = %s\n", s_Identification.serialNumber.c_str());
    CIpodLog::d("firmwareVersion = %s\n", s_Identification.firmwareVersion.c_str());
    CIpodLog::d("hardwareVersion = %s\n", s_Identification.hardwareVersion.c_str());
    CIpodLog::d("messages.value = %lu\n", s_Identification.messages.value);
    CIpodLog::d("power = %d\n", s_Identification.power);
    CIpodLog::d("maxCurrDrawn = %d\n", s_Identification.maxCurrDrawn);
    CIpodLog::d("productPlanUID = %s\n", s_Identification.productPlanUID.c_str());
    if (s_Identification.eaprotocol.id > 0) {
        CIpodLog::d("eaprotocol: id = %u, name = %s, action = %d, nativeid = %u, carplay = %d\n",
                s_Identification.eaprotocol.id, s_Identification.eaprotocol.name.c_str(), s_Identification.eaprotocol.action, s_Identification.eaprotocol.nativeid, s_Identification.eaprotocol.carplay);
    }
    CIpodLog::d("appTeamId = %s\n", s_Identification.appTeamId.c_str());
    CIpodLog::d("currentLanguage = %s\n", s_Identification.currentLanguage.c_str());
    for (std::list<std::string>::iterator iter = s_Identification.supportedLanguages.begin(); iter != s_Identification.supportedLanguages.end(); ++iter) {
        CIpodLog::d("supportedLanguages: %s\n", iter->c_str());
    }
    if (s_Identification.serialTransport.id > 0) {
        CIpodLog::d("serialTransport: id = %u, name = %s, connection = %d\n",
                s_Identification.serialTransport.id, s_Identification.serialTransport.name.c_str(), s_Identification.serialTransport.connection);
    }
    if (s_Identification.usbDeviceTransport.id > 0) {
        CIpodLog::d("usbDeviceTransport: id = %u, name = %s, connection = %d, rates = %d\n",
                s_Identification.usbDeviceTransport.id, s_Identification.usbDeviceTransport.name.c_str(), s_Identification.usbDeviceTransport.connection, s_Identification.usbDeviceTransport.rates.size());
    }
    if (s_Identification.usbHostTransport.id > 0) {
        CIpodLog::d("usbHostTransport: id = %u, name = %s, connection = %d, number = %u, carplay = %d\n",
                s_Identification.usbHostTransport.id, s_Identification.usbHostTransport.name.c_str(), s_Identification.usbHostTransport.connection, s_Identification.usbHostTransport.number, s_Identification.usbHostTransport.carplay);
    }
    if (s_Identification.bluetoothTransport.id > 0) {
        CIpodLog::d("bluetoothTransport: id = %u, name = %s, connection = %d\n",
                s_Identification.bluetoothTransport.id, s_Identification.bluetoothTransport.name.c_str(), s_Identification.bluetoothTransport.connection);
    }
    for (std::list<USBDeviceHIDComponent>::iterator iter = s_Identification.usbDeviceHIDs.begin(); iter != s_Identification.usbDeviceHIDs.end(); ++iter) {
        CIpodLog::d("usbDeviceHIDs: id = %u, name = %s, function = %d\n",
                iter->id, iter->name.c_str(), iter->function);
    }
    if (s_Identification.vehicleInfo.id > 0) {
        CIpodLog::d("vehicleInfo: id = %u, name = %s, engines = %d, display = %s\n",
                s_Identification.vehicleInfo.id, s_Identification.vehicleInfo.name.c_str(), s_Identification.vehicleInfo.engines.size(), s_Identification.vehicleInfo.display.c_str());
    }
    if (s_Identification.vehicleStatus.id > 0) {
        CIpodLog::d("vehicleStatus: id = %u, name = %s, range = %d, temperature = %d, warning = %d\n",
                s_Identification.vehicleStatus.id, s_Identification.vehicleStatus.name.c_str(), s_Identification.vehicleStatus.range, s_Identification.vehicleStatus.temperature, s_Identification.vehicleStatus.warning);
    }
    if (s_Identification.locationInfo.id > 0) {
        CIpodLog::d("locationInfo: id = %u, name = %s, GPGGA = %d, GPRMC = %d, GPGSV = %d, PASCD = %d, PAGCD = %d, PAACD = %d, GPHDT = %d\n",
                s_Identification.locationInfo.id, s_Identification.locationInfo.name.c_str(), s_Identification.locationInfo.GPGGA, s_Identification.locationInfo.GPRMC, s_Identification.locationInfo.GPGSV, s_Identification.locationInfo.PASCD, s_Identification.locationInfo.PAGCD, s_Identification.locationInfo.PAACD, s_Identification.locationInfo.GPHDT);
    }
    for (std::list<USBHostHIDComponent>::iterator iter = s_Identification.usbHostHIDs.begin(); iter != s_Identification.usbHostHIDs.end(); ++iter) {
        CIpodLog::d("usbHostHIDs: id = %u, name = %s, function = %d, transportid = %u, number = %u\n",
                iter->id, iter->name.c_str(), iter->function, iter->transportid, iter->number);
    }
    for (std::list<WirelessCarPlayTransportComponent>::iterator iter = s_Identification.wirelessCarPlayTransports.begin(); iter != s_Identification.wirelessCarPlayTransports.end(); ++iter) {
        CIpodLog::d("wirelessCarPlayTransports: id = %u, name = %s, connection = %d, carplay = %u\n", iter->id, iter->name.c_str(), iter->connection, iter->carplay);
    }
    if (s_Identification.bluetoothHID.id > 0) {
        CIpodLog::d("bluetoothHID: id = %u, name = %s, function = %d, transportid = %u\n",
                s_Identification.bluetoothHID.id, s_Identification.bluetoothHID.name.c_str(), s_Identification.bluetoothHID.function, s_Identification.bluetoothHID.transportid);
    }
    if (s_Identification.routeGuidance.id > 0) {
        CIpodLog::d("routeGuidance: id = %u, name = %s, MaxGuiManeStoCap = %u\n",
                s_Identification.routeGuidance.id, s_Identification.routeGuidance.name.c_str(), s_Identification.routeGuidance.maxGuiManeStoCap);
    }
    CIpodLog::d("CIpodConfig::debugConfig, end -----------------------------------\n");
}
